//
//  WBStatusOriginalView.swift
//  Weibo
//
//  Created by xu on 2017/3/24.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit
import SnapKit

class WBStatusOriginalView: UIView {

   var originalViewBottomConstraint: Constraint?
    // 提供一个数据(statusViewModel) 供外界赋值
    var statusViewModel:WBStatusViewModel? {
        
        didSet {
            // 给其身上的子控件进行赋值
             //头像赋值
            headImageView.my_setImage(urlString: statusViewModel?.homeModel?.user?.profile_image_url, placeholderImgName: "avatar_default")
            // 昵称
            nameLabel.text = statusViewModel?.homeModel?.user?.name
            // 会员等级
            membershipImageView.image = statusViewModel?.mbrankImage
            // 认证
            avatarImageView.image = statusViewModel?.verifiedImage
            // 微博正文
            contentLabel.text = statusViewModel?.homeModel?.text
            //微博时间?
            timeLabel.text = statusViewModel?.createAtText
            //微博来源赋值
            sourceLabel.text = statusViewModel?.sourceText
          
            // 微博正文
            contentLabel.attributedText = statusViewModel?.originalAttributedString
            //卸载约束
            originalViewBottomConstraint?.deactivate()
            
             // 来判断是否显示配图 判断是否为nil 而且是否count > 0
            if let picUrls = statusViewModel?.homeModel?.pic_urls, picUrls.count > 0{
                // 代表有配图
                // 赋值
                pictureView.picUrls = picUrls
                //重设置 自己(微博原创)的约束
                self.snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })

                pictureView.isHidden = false
                
            }else{
                self.snp.makeConstraints({ (make) in
                originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
            })

                pictureView.isHidden = true
            }
        }
    }

    
    
    //重写父类构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置视图

     private func setupUI(){        
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        headImageView.snp.makeConstraints { (make) in
            make.left.top.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView.snp.right).offset(10)
            make.top.equalTo(headImageView)
        }
        
        membershipImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp.right).offset(10)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp.right).offset(10)
            make.top.equalTo(timeLabel)
        }
        
        avatarImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp.right)
            make.centerY.equalTo(headImageView.snp.bottom)
        }

        
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView)
            make.top.equalTo(headImageView.snp.bottom).offset(10)
            make.width.equalTo(ScreenWidth - 20)

        }
        
       
        pictureView.snp.makeConstraints { (make) in
           
            make.left.equalTo(self).offset(10)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
        }
        
        //给自己设置位置,这样实现自动计算行高
        self.snp.makeConstraints { (make) in
          originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        //监听label的高亮点击事件
        contentLabel.highlightTapAction = {(containerView,text,range,rect)->() in
            
       
      }
    
    }
    
    //MARK: 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(title: "昵称", fontSize: 14, textColor: UIColor.black)
    // 等级
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    // 时间
    private lazy var timeLabel: UILabel = UILabel(title: "微博时间", fontSize: 10, textColor: UIColor.orange)
    // 微博来源
    private lazy var sourceLabel: UILabel = UILabel(title: "微博来源", fontSize: 10, textColor: UIColor.darkGray)
    // 微博认证
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    
    //微博正文
    private lazy var contentLabel: YYLabel = {
        
        let modifier = YYTextLinePositionSimpleModifier()
        modifier.fixedLineHeight = 22
        let lab = YYLabel()
        lab.linePositionModifier = modifier
        lab.numberOfLines = 0
        // 设置最大显示宽度

        lab.preferredMaxLayoutWidth = ScreenWidth - 20
        
        return lab

    }()
    //collectionView-- 原创配图
    private lazy var pictureView:WBStatusPictureView = WBStatusPictureView()


}
